package metric;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import simulationEngine.SimulationEngine;
import databaseConnect.SimulationExecutionWriteProcessor;
import datatypes.MetricObject;

public class CentralObserver extends Observable implements Observer {

	//Variables
	private int uSimID = -1;
	//Singleton
	private static CentralObserver instance = null;
	
	private CentralObserver(int uniqueSimID){
		System.out.println(uniqueSimID + "");
		this.uSimID = uniqueSimID;
	};
	
	public static CentralObserver getInstance(int UniqueSimID){
		if (instance == null){
			instance = new CentralObserver(UniqueSimID);
		}
		return instance;
	}
	
	//SingletonEnde
	@Override
	public void update(Observable arg0, Object arg1) {
		//System.out.println("Metric:" + ((MetricObject)arg1).getMetricType()+ " Stufe:" + ((MetricObject)arg1).getTimeStep() + " Device: " + ((MetricObject)arg1).getContent().get(2));
		MetricObject tmp = ((MetricObject)arg1);
		///////////DB
		SimulationExecutionWriteProcessor.writeMetricContent(this.uSimID, "1", tmp);

		
		/////////GUI
		Vector<String> newRow = new Vector<String>();
		newRow.add(tmp.getMetricName() + ":" + tmp.getMetricType());
		newRow.add(tmp.getTimeStep() + "");
		newRow.add(tmp.getContent().get(2));
		if (tmp.getContent().size() > 3){
			newRow.add(tmp.getContent().get(3));			
		} else {
			newRow.add("");
		}
		SimulationEngine.getEngine(Integer.parseInt(((MetricObject)arg1).getContent().get(0))).getGUI().getStatisticCard().addNewMetricRow(newRow);
	}

}
